﻿using CPPEI.ETL.DataServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.DSImplement
{
    public class RDSDtSerProxy : IDataServiceProxy, IDetectable, IFeedback, IColumnEnumerator
    {
        public DataServiceInterface.TDataService GetSchema(DataServiceInterface.DataServiceInfo info)
        {
            DataServiceInterface.TDataService dataService = new TDataService();
            dataService.Name = info.Address;
            dataService.Address = info.Address;
            return dataService;
        }

        public DataServiceInterface.TDataSource GetSchema(DataServiceInterface.DataSourceInfo info)
        {
            WebServiceRef.RawDataService service = new WebServiceRef.RawDataService();
            service.Url = info.Address;

            Guid appId = new Guid(info.AuthUser);
            string token = info.AuthPassword;

            Guid tableId = new Guid(info.DataSourceValue);

            WebServiceRef.RawDataTableColumn[] columns = service.GetSchema(appId, token, tableId);

            DataServiceInterface.TDataSource tSource = new TDataSource();
            tSource.Name = info.DataSource;
            tSource.Type = "Table";

            for (int index = 0; index < columns.Length; index++)
            {
                Type dataType = CPPEI.ETL.Infrastructure.TypeUtil.ConvertFromDbType(columns[index].DbType);
                DataServiceInterface.TDataSourceColumn tColumn = new TDataSourceColumn(columns[index].Name, dataType);

                tSource.Columns.Add(tColumn);
            }
            return tSource;
        }

        public DataServiceInterface.IData GetData(DataServiceInterface.DataSourceInfo info, Dictionary<string, object> args)
        {
            WebServiceRef.RawDataService service = new WebServiceRef.RawDataService();
            service.Url = info.Address;

            Guid appId = new Guid(info.AuthUser);
            string token = info.AuthPassword;

            Guid tableId = new Guid(info.DataSourceValue);

            DataTable dataTable = service.GetData(appId, token, tableId, false);

            return new DataServiceInterface.DataTableData(dataTable);
        }

        public List<string> Enumerate(DataServiceInterface.DataServiceInfo info)
        {
            WebServiceRef.RawDataService service = new WebServiceRef.RawDataService();
            service.Url = info.Address;

            WebServiceRef.RawDataTable[] tables = service.Enumerate();

            List<string> tableNames = new List<string>();
            for (int index = 0; index < tables.Length; index++)
            {
                tableNames.Add(tables[index].Name);
            }
            return tableNames;
        }

        public List<TDataSource> EnumerateSources(DataServiceInfo service)
        {
            WebServiceRef.RawDataService rDataService = new WebServiceRef.RawDataService();
            rDataService.Url = service.Address;

            WebServiceRef.RawDataTable[] tables = rDataService.Enumerate();

            List<TDataSource> sources = new List<TDataSource>();
            for (int index = 0; index < tables.Length; index++)
            {
                TDataSource source = new TDataSource();
                source.Name = tables[index].Name;
                source.Value = tables[index].Id.ToString();
                source.Description = tables[index].Description;

                sources.Add(source);
            }
            return sources;
        }

        public List<TDataSourceParameter> EnumeratePrameters(DataSourceInfo source)
        {
            throw new NotImplementedException();
        }

        #region Implement IFeedback
        public void Feedback(DataSourceInfo source, Guid batchId, Guid sourceId, long dataCount, Guid[] passedData, Guid[] unpassedData)
        {
            WebServiceRef.RawDataService service = new WebServiceRef.RawDataService();
            service.Url = source.Address;

            Guid appId = new Guid(source.AuthUser);
            string token = source.AuthPassword;

            Guid tableId = new Guid(source.DataSourceValue);

            service.Feedback(appId, token, tableId, batchId, (int)dataCount, passedData, unpassedData);
        }
        #endregion

        #region Implement IColumnEnumerator
        public List<TDataSourceColumn> GetColumns(DataSourceInfo source)
        {
            WebServiceRef.RawDataService service = new WebServiceRef.RawDataService();
            service.Url = source.Address;

            Guid appId = new Guid(source.AuthUser);
            string token = source.AuthPassword;

            Guid tableId = new Guid(source.DataSourceValue);

            List<TDataSourceColumn> retColumns = new List<TDataSourceColumn>();
            WebServiceRef.RawDataTableColumn[] columns = service.GetSchema(appId, token, tableId);
            for (int index = 0; index < columns.Length; index++)
            {
                TDataSourceColumn dsColumn = new TDataSourceColumn(columns[index].Name, CPPEI.ETL.Infrastructure.TypeUtil.ConvertFromDbType(columns[index].DbType));
                retColumns.Add(dsColumn);
            }
            return retColumns;
        }

        public List<TDataSourceColumn> EnumerateColumns(DataSourceInfo source)
        {
            throw new NotImplementedException();
        }
        #endregion


    }
}
